/*
// Description
// ----------------------------
This program creates binscatters of covariates at the discontinuity between IOU and Municipal electricity service territories. */

set scheme s1color

/*** START CODE ***/


// Preamble
// ----------------------------
capture log close
clear all
*version 14.2
set more off
set matsize 800



/*Binscatters proximate to utility boundary discontinuity*/

use "${Data_Clean}/spatial_RD_regdata.dta", clear

local delta_demog "delta_population delta_pop_density delta_income"
local delta_dist "cons i.groupid#c.IOU_dist i.groupid#c.muni_dist"
local other_cov "delta_fuel_economy_mean delta_hybrid_share delta_luxury_share delta_mud_hh_share"
local iou_fe "PGE_dum SCE_dum SDGE_dum"

local gas_price_var "delta_obswgtprice_lt3_"
local elec_price_var "delta_p_e_active"
local price_var "`elec_price_var' `gas_price_var'"

local dist_km = 8
local dist_m = `dist_km'*1000
local dist_condition "distance_btw_cbg0 < `dist_m'"


estimates clear

reghdfe delta_share_BEV `price_var' `delta_demog' `other_cov', absorb(`delta_dist') vce(cluster cbg0 cbg1)
gen insample = e(sample)
gen nomissingutil = (utility_name0 ~= "" & utility_name1 ~= "")
gen missingBEVshare	= (delta_share_BEV == .)
gen missingelecprice = (delta_p_e_active == .)
gen missinggasprice = (delta_obswgtprice_lt3_ == .)


reghdfe delta_share_BEV `price_var' `delta_demog' `other_cov' if `dist_condition', absorb(`delta_dist') vce(cluster cbg0 cbg1)
gen insample_dist = e(sample)

*Note: some fraction of ACS census block groups have zero population.  
tab insample nomissingutil
tab insample missingBEVshare
tab insample missingelecprice
tab insample missinggasprice


gen IOU_name = ""
replace IOU_name = utility_name0 if ref_IOU==1
replace IOU_name = utility_name1 if ref_IOU==0
gen Muni_name = ""
replace Muni_name = utility_name1 if ref_IOU==1
replace Muni_name = utility_name0 if ref_IOU==0
tab IOU_name
tab Muni_name


*Graphs of differential Population Density and MUD by MUNI / IOU
preserve

*Define differentials where differentials are Muni Demographic - IOU demographic
gen mud_hh_diff = .
replace mud_hh_diff = delta_mud_hh_share if ref_IOU==0
replace mud_hh_diff = -delta_mud_hh_share if ref_IOU==1

gen pop_dens_diff = .
replace pop_dens_diff = delta_pop_density if ref_IOU==0
replace pop_dens_diff = -delta_pop_density if ref_IOU==1

gen totalpop = population0+population1

collapse (mean) pop_dens_diff mud_hh_diff, by(Muni_name)
sum pop_dens, det
scalar PDcut = r(p75)
sum mud_hh_diff, det
scalar mudcut = r(p75)
macro list
scalar list
gen density_subset = (mud_hh_diff<mudcut) & (pop_dens_diff<PDcut)

tempfile bordersubset
keep Muni_name density_subset
save `bordersubset', replace

restore

merge n:1 Muni_name using `bordersubset'
tab _merge
drop _merge

keep utility* region* Q_BEV* Q_PHEV* cbg* distance* income* population* area* fuel_economy* pop_density* total_hh* hybrid_share* luxury_share* mud_hh* P_gas* obswgtprice_lt3_* p_e_active* ref_IOU adj_IOU share_BEV* groupid group_pair_id year insample insample_dist nomissingutil density_subset

reshape long utility_name region Q_BEV Q_PHEV cbg distance_btw_cbg distance_util income population area fuel_economy_mean pop_density total_hh hybrid_share luxury_share obswgtprice_lt3_ mud_hh_share P_gas p_e_active share_BEV, i(groupid ref_IOU adj_IOU group_pair_id year insample insample_dist nomissingutil density_subset) j(ref_dum)

gen IOU_dum = ref_IOU if ref_dum == 0
replace IOU_dum = adj_IOU if ref_dum == 1
order IOU_dum cbg utility_name
tab utility_name, sum(IOU_dum)

drop ref_IOU adj_IOU ref_dum
sort cbg year

*Change distance to boundary from meters to kms
replace distance_util = distance_util/1000

*Consolidate duplicates
duplicates drop

*Set distance where negative is distance on IOU side, and positive is distance on Muni side.
gen border_distance = .
replace border_distance = -distance_util if IOU_dum == 1
replace border_distance = distance_util if IOU_dum == 0 
sum border_distance

local insample_cond "insample==1"
macro list

*Histogram of distance to border_distance
graph twoway histogram border_distance if border_distance<=5 & border_distance>=-5, width(.1) xline(0)


*Figure 3a - 3g: Discontinuities at border, insample observations, fourth degree polynomial, fixed effects for each border. 
rename p_e_active elec_price
rename obswgtprice_lt3_ gas_price
xi i.groupid

local options "covs(_Igroupid_*)" 
label var income "Mean HH Income ($)"
foreach x in income population pop_density hybrid_share mud_hh_share luxury_share fuel_economy_mean {
	reg `x' i.groupid
	predict `x'_resid, resid
		
	rdplot `x' border_distance if `insample_cond' & border_distance<=5 & border_distance>=-5 , `options' /// 
	graph_options( xtitle("Distance to utility boundary (km)") ///
	legend(off)) 
	graph export "$ResultsOut/Figures/`x'_insample_5k.png", replace
}


*Figure 1: Generate discontinuity figure for electricity
rdplot elec_price border_distance if insample==1 & border_distance<=5 & border_distance>=-5, nbins(20 20) /// 
	graph_options( xtitle("Distance to utility boundary (km)") ///
	ytitle("Electricity Prices($/kwh)") /// 
	legend(off)) 
	graph export "$ResultsOut/Figures/Electricity_RD_graph_5k.png", replace


*Regressions of discontinuity

gen cbgid = group(cbg)

forvalues x = 4/4 {
	preserve
	forvalues y = 1 / `x' {
		gen IOU_dist`y' = 0
		replace IOU_dist`y' = border_distance^`y' if IOU_dum == 1 
		gen Muni_dist`y' = 0
		replace Muni_dist`y' = border_distance^`y' if IOU_dum == 0
	}
	
foreach z in income population pop_density hybrid_share mud_hh_share luxury_share fuel_economy_mean elec_price gas_price {
		display " "
		display " "
		display "`z' - Polynomial `x'"
		
		qui reghdfe `z' IOU_dum IOU_dist* Muni_dist* if insample==1, absorb(groupid) vce(cluster groupid)
		test IOU_dum
		
		qui reghdfe `z' IOU_dum IOU_dist* Muni_dist* if insample==1 & density_subset==1, absorb(groupid) vce(cluster groupid)
		test IOU_dum		
		
		qui reghdfe `z' IOU_dum IOU_dist* Muni_dist* if `insample_cond' & border_distance<=5 & border_distance>=-5 , absorb(groupid) vce(cluster groupid)
		test IOU_dum
				
		qui reghdfe `z' IOU_dum IOU_dist* Muni_dist* if `insample_cond' & border_distance<=5 & border_distance>=-5 & density_subset==1, absorb(groupid) vce(cluster groupid)
		test IOU_dum
		
}
		restore	
}

	

